package com.wwj.my_ummary.user.controller;


import com.wwj.my_ummary.enmu.GenderEnum;
import com.wwj.my_ummary.user.entity.User;
import com.wwj.my_ummary.user.service.UserService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wwj
 */
@RestController
@RequestMapping("/user")
public class UserController {


    @Resource
    private UserService userService;
    @Resource
    private RedisTemplate redisTemplate;


    @RequestMapping("/show")
    public String show(HttpServletRequest request) {
        return "I'm " + request.getLocalPort();
    }


    @RequestMapping(value = "/session")
    public String getSession(HttpServletRequest request) {

        redisTemplate.boundValueOps("dsf").set("user");

        Object dsf = redisTemplate.boundValueOps("dsf").get();

        return dsf.toString();
    }

    @RequestMapping(value = "/get")
    public String get(HttpServletRequest request) {
        String token = (String) request.getSession().getAttribute("token1");


        if (token == null) {
            request.getSession().setAttribute("token1", "admin1");
            token = (String) request.getSession().getAttribute("token1");
        }
        return "I'm " + request.getLocalPort() + " token1 :" + token;
    }


    @RequestMapping("test")
    @ResponseBody
    public List selUser() {
        List<User> users = userService.selectList();
        return users;
    }


    @RequestMapping("addUser")
    @ResponseBody
    public int addUser() {
        User user = new User();
        user.setUser("123");
        user.setPassword("456");
        user.setSex(GenderEnum.other);
        int insert = userService.insert(user);
        return insert;
    }


}
